<?php
class BlogController extends Zend_Controller_Action
{
    public function indexAction ()
    {    
        $blogService = Service_Blog::getInstance();
        $this->view->blogs = $blogService->getAllBlogs();
        $this->view->form = new Form_Blog();
    }
    
    public function viewAction()
    {
        $blogService = Service_Blog::getInstance();
        $blog = $blogService->findBlog((integer) $this->_getParam('id',null));
        $postService = Service_Post::getInstance();
        $posts = $postService->getPostsByPage($blog, (integer) $this->_getParam('page', 0), $itemCountPerPage = 30);
        $this->view->assign(array('blog'=>$blog, 'posts'=>$posts));
    }
    
    public function updateAction()
    {
        $service = Service_Blog::getInstance();
        $blogId = $this->_getParam('id',null);
        $form = new Form_Blog($service->findBlog($blogId));
        if($this->_request->isPost()){
            if($form->isValid($this->getRequest()->getPost())){
                $blog = new Model_Blog($this->getRequest()->getPost());
                $blog->id = $blogId;
                $blog = $service->saveBlog($blog);
                $this->_redirect('/blog/view/id/'. $blog->id);
            }
        }
        $this->view->form = $form;
    }
    
    public function deleteAction()
    {
         $service = Service_Blog::getInstance()->deleteBlog((integer)$this->_getParam('id'));
         $this->_redirect('/blog/');
    }
}

